from manim import *

class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [0, 10, 0],
            [-1,9,0],
            [-2,2,0],
            [-5, 0, 0],
            [-2,-2,0],
            [-1,-9,0],
            [0, -10, 0],
        ]
        self.set_points_smoothly(points)
class CustomCurve1(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [35, 30, 0],
            [34,29,0],
            [33,23,0],
            [30, 20, 0],
            [33,18,0],
            [34,11,0],
            [35, 10, 0],
        ]
        self.set_points_smoothly(points)

class CustomCurve2(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [35, -15, 0],
            [30,-20,0],
            [35,-25,0],
            
        ]
        self.set_points_smoothly(points)

class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        self.play(self.camera.frame.animate.scale(1.65).move_to([28,0,0]))
        #题目出现
        text = Tex(r"$y''+py+q=f\left( x\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,0,0])
        text1 = Tex(
                   r"\text{微分方程}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text,DOWN,buff=1)
        arrow = Arrow(start=[-29,0,0],end=[-23,0,0],color=PURE_GREEN
                       , stroke_width=20, tip_length=1)

        
        text2 = Tex(r"\text{齐通+非齐特}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,0,0])
        

        self.play(Write(text),Write(text1),Write(text2),GrowArrow(arrow))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
       
        
        curve = CustomCurve(color=RED, stroke_width=20)

        self.play(Create(curve))

        text4 = Tex(r"\text{若：}$f\left( x\right) =\left( ax+b\right) e^{kx}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,10,0])
        
        text5 = Tex(r"\text{若：}$f\left( x\right) =e^{\alpha x}\left( \left( ax+b\right) \sin \beta x+\left( cx+d\right) \cos \beta x\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-10,0])
        
        self.play(Write(text4),Write(text5))
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-20.5,0,0])
        self.play(Create(rectangle))
        arrow = Arrow(start=[-20.5,4,0],end=[-20.5,10,0],color=PURE_BLUE
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow))
        arrow = Arrow(start=[-20.5,-4,0],end=[-20.5,-10,0],color=PURE_BLUE
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow))

        text6 = Tex(r"$\lambda ^{2}+p\lambda +q=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20.5,16,0])
        text7 = Tex(
                   r"\text{特征方程}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text6,DOWN,buff=1)
        self.play(Write(text6),Write(text7))
        text8 = Tex(r"\text{得}$\lambda _{1},\lambda _{2}$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20.5,20,0])
        self.play(Write(text8))
        arrow = Arrow(start=[-20.5,23,0],end=[-20.5,30,0],color=PURE_BLUE
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow))
        text9 = Tex(r"\text{齐通的三种解形式}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20.5,35,0])
        self.play(Write(text9))

        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=9,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-10,0,0])
        self.play(Create(rectangle))
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=9,height=4
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-20.5,20,0])
        self.play(Create(rectangle))
        arrow = Arrow(start=[-15,20,0],end=[22,20,0],color=PURE_GREEN
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow))
        arrow = Arrow(start=[27.5,13,0],end=[27.5,17.5,0],color=PURE_GREEN
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow))

        text10 = Tex(r"\text{对比}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([27,20,0])
        self.play(Write(text10))

        






        text6 = Tex(r"$\lambda ^{2}+p\lambda +q=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20.5,-16,0])
        text7 = Tex(
                   r"\text{特征方程}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text6,UP,buff=1)
        self.play(Write(text6),Write(text7))
        text8 = Tex(r"\text{得}$\lambda _{1,2}=\alpha _{0}+\beta _{0}i$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20.5,-20,0])
        self.play(Write(text8))
        arrow = Arrow(start=[-20.5,-23,0],end=[-20.5,-30,0],color=PURE_BLUE
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow))
        text9 = Tex(r"\text{齐通的三种解形式}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20.5,-35,0])
        self.play(Write(text9))

        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=9,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-10,0,0])
        self.play(Create(rectangle))
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=19,height=4
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-20.5,-20,0])
        self.play(Create(rectangle))
        arrow = Arrow(start=[-10,-20,0],end=[22,-20,0],color=PURE_GREEN
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow))
        arrow = Arrow(start=[27.5,-13,0],end=[27.5,-17.5,0],color=PURE_GREEN
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow))

        text10 = Tex(r"\text{对比}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([27,-20,0])
        self.play(Write(text10))













        curve1 = CustomCurve1(color=RED, stroke_width=20)

        self.play(Create(curve1))
        text11 = Tex(r"\text{若：}$k\neq \lambda _{1}$\text{且}$k\neq \lambda _{2}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([46,30,0])
        text12 = Tex(r"\text{若：}$k\neq \lambda _{1}$\text{或}$k\neq \lambda _{2}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([46,20,0])
        text13 = Tex(r"\text{若：}$k=\lambda _{1}=\lambda _{2}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([45,10,0])
        self.play(Write(text13),Write(text11),Write(text12))
        text111 = Tex(r"\text{设}$y^{\ast }=\left( cx+d\right) e^{kx}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text11,RIGHT,buff=5)
        text121 = Tex(r"\text{设}$y^{\ast }=x\left( cx+d\right) e^{kx}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text12,RIGHT,buff=5)
        text131 = Tex(r"\text{设}$y^{\ast }=x^{2}\left( cx+d\right) e^{kx}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text13,RIGHT,buff=5)
        self.play(Write(text131),Write(text111),Write(text121))
        curve2 = CustomCurve2(color=RED, stroke_width=20)

        self.play(Create(curve2))



        text11 = Tex(r"\text{若：}$\lambda _{1,2}\neq \alpha +\beta i$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([46,-15,0])
        
        text13 = Tex(r"\text{若：}$\lambda _{1,2}=\alpha +\beta i$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([45,-25,0])
        self.play(Write(text13),Write(text11))
        text111 = Tex(r"\text{设}$y^{\ast }=e^{\alpha x}\left[ \left( mx+n\right) \cos \beta x+\left( px+q\right) \sin \beta x\right] $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text11,RIGHT,buff=1)
        text131 = Tex(r"\text{设}$y^{\ast }=xe^{\alpha x}\left[ \left( mx+n\right) \cos \beta x+\left( px+q\right) \sin \beta x\right] $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text13,RIGHT,buff=1)
        self.play(Write(text131),Write(text111))





        self.wait()